<?php
/**
 * 店铺地址
 *
 *
 *
 *
 * @copyright  Copyright (c) 中国追溯
 * @since      File available since Release v1.1
 */
use Shopnc\Tpl;


defined('InShopNC') or exit('Access Invalid!');
class store_myControl extends BaseSellerControl {
    public function __construct() {
        parent::__construct();
    }

    /**
     * 店铺地址地图显示
     *
     */
    public function indexOp() {
        //读取语言包
        Language::read('transport');

        $model_store_map = Model('store_map');
        $store_id = $_SESSION['store_id'];
        $condition = array();
        $condition['store_map.store_id'] = $store_id;
        $fields = array(
            'store_map.map_id',
            'store_map.store_id',
            'store_map.name_info',
            'store_map.bus_info',
            'store_map.phone_info',
            'store_map.address_info',
            'store_map.baidu_lng',
            'store_map.baidu_lat',
            'store_map.baidu_province',
            'store_map.baidu_city',
            'store_map.baidu_district',
            'store_map.baidu_street',
            'store_map_delivery.delivery_id',
            'store_map_delivery.state',
            'store_map_delivery.delivery_start_time',
            'store_map_delivery.delivery_end_time',
            'store_map_delivery.district_id',
            'store_map_delivery.district_name',
            'store_map_delivery.order_floor_price',
            'store_map_delivery.first_weight_freight',
            'store_map_delivery.incr_freight',
            'store_map_delivery.order_freight',
            'store_map_delivery.freight_type',
        );
        $map_delivery = $model_store_map->getStoreMapDelivery($condition,$fields);
        Tpl::output('map_delivery',$map_delivery);
        self::profile_menu('store_my','index');

        $areas = Model('area')->getAreas();
        Tpl::output('areas', $areas);
//        pr($areas);

        Tpl::showpage('store_my.index');
    }

    /**
     * 编辑店铺地址
     *
     */
    public function edit_mapOp() {
        Tpl::showpage('store_my.edit_map','null_layout');
    }

    public function save_map_deliveryOp(){
//        pr($_POST);

        $model_store_map = Model('store_map');
        $model_store_map->beginTransaction();
        $map_id = $_POST['map_id'];

        $map_condition = array(
            'store_id' => $_SESSION['store_id'],
            'map_id' => $map_id,
        );
        $map_array = array();
        $map_array['name_info'] = $_POST['name_info'];
        $map_array['address_info'] = $_POST['address_info'];
        $map_array['phone_info'] = $_POST['phone_info'];
        $map_array['bus_info'] = $_POST['bus_info'];
        $map_array['update_time'] = time();
        $map_array['baidu_province'] = $_POST['baidu_province'];
        $map_array['baidu_city'] = $_POST['baidu_city'];
        $map_array['baidu_district'] = $_POST['baidu_district'];
        $map_array['baidu_street'] = $_POST['baidu_street'];
        $map_array['baidu_lng'] = $_POST['baidu_lng'];
        $map_array['baidu_lat'] = $_POST['baidu_lat'];
        /*pr($map_condition,0);
        pr($map_array);*/
        //保存
        if($map_id){
            //修改
            $map_update = $model_store_map->editStoreMap($map_condition, $map_array);
        }else{
            //添加
            $model_store = Model('store');
            $store = $model_store->getStoreInfoByID($_SESSION['store_id']);
            $map_array['store_id'] = $store['store_id'];
            $map_array['sc_id'] = $store['sc_id'];
            $map_array['store_name'] = $store['store_name'];
            $map_update = $model_store_map->addStoreMap($map_array);
            $map_id = $map_update;
        }


        $delivery_id = $_POST['delivery_id'];
        $delivery_condition = $map_condition;
        $delivery_condition['delivery_id'] = $delivery_id;
        $areas = explode('|||',$_POST['areas']);
        $district_id = $areas[0];
        $district_name = $areas[1];
        $delivery_array = array();
        $delivery_array['state'] = $_POST['state'];
        $delivery_array['delivery_start_time'] = $_POST['delivery_start_time'];
        $delivery_array['delivery_end_time'] = $_POST['delivery_end_time'];
        $delivery_array['district_id'] = $district_id;
        $delivery_array['district_name'] = $district_name;
        $delivery_array['order_floor_price'] = $_POST['order_floor_price'];
        $delivery_array['first_weight_freight'] = $_POST['first_weight_freight'];
        $delivery_array['incr_freight'] = $_POST['incr_freight'];
        $delivery_array['order_freight'] = $_POST['order_freight'];
        $delivery_array['freight_type'] = $_POST['freight_type'];
//        pr($delivery_condition,0);
//        pr($delivery_array);
        if($delivery_id){
            //修改
            $delivery_update = $model_store_map->editStoreMapDelivery($delivery_condition, $delivery_array);
        }else{
            //添加
            $delivery_array['map_id'] = $map_id;
            $delivery_array['store_id'] = $_SESSION['store_id'];
            $delivery_update = $model_store_map->addStoreMapDelivery($delivery_array);
        }

        if((false!==$map_update) && (false!==$delivery_update)){
            $model_store_map->commit();
            showDialog(Language::get('nc_common_save_succ'),'reload','succ');
        }else{
            $model_store_map->rollback();
            showDialog(Language::get('nc_common_save_fail'),'reload','error');
        }
    }

    /**
     * 用户中心右边，小导航
     *
     * @param string    $menu_type  导航类型
     * @param string    $menu_key   当前导航的menu_key
     * @return
     */
    private function profile_menu($menu_type,$menu_key='') {
        $menu_array = array();
        switch ($menu_type) {
            case 'store_my':
                $menu_array = array(
                    array('menu_key'=>'index','menu_name'=>'配送设置 ',  'menu_url'=>'index.php?act=store_my&op=index'),
                );
                break;
        }
        Tpl::output('member_menu',$menu_array);
        Tpl::output('menu_key',$menu_key);
    }

}